package org.jeecg.modules.lab.util;


import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.operation.entity.LabOperation;
import org.jeecg.modules.lab.operation.service.ILabOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Slf4j
public class BeanChangeUtil<T> {

    @Autowired
    private ILabOperationService labOperationService;
    @Autowired
    private ILabCronJobService labCronJobService;


    public String contrastObj(Object oldBean, Object newBean) {
        StringBuilder str = new StringBuilder();
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        try {
            // 通过反射获取类的类类型及字段属性
            Class clazz = pojo1.getClass();
            Field[] fields = clazz.getDeclaredFields();
            FilterUtils filterUtils =new FilterUtils();
            int i = 1;
            for (Field field : fields) {
                // 排除序列化属性
                if ("serialVersionUID".equals(field.getName()) || filterUtils.filterEntrust(field.getName()) ) {
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                // 获取对应属性值
                Method getMethod = pd.getReadMethod();
                Object o1 = getMethod.invoke(pojo1);
                Object o2 = getMethod.invoke(pojo2);
                if (o1 == null || o2 == null) {
                    continue;
                }
                if (!o1.toString().equals(o2.toString())) {
                    str.append(i + "、" + field.getAnnotation(ApiModelProperty.class).value() + ":由[" + o1 + "]变更成[" + o2 + "]; ");
                    i++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    public void saveOperation(LoginUser loginUser,Object oldBean, Object newBean,String id,String tableName,String type) {

        //记录修改前后数据
        String str =contrastObj(oldBean, newBean);
        // 如果 没有修改单据信息不用记录日志
        if(str != null  && !"".equals(str.trim())){
            LabOperation labOperation = new LabOperation();
            labOperation.setOperationDate(DateUtils.getDate());
            labOperation.setUserId(loginUser.getId());
            labOperation.setUserName(loginUser.getUsername());
            labOperation.setTableId(id);
            labOperation.setDescription(str);
            labOperation.setTableName(tableName);
            labOperation.setType(type);
            labOperationService.save(labOperation);

            if (MyEnum.sync) {
                //同步表信息
                long time = new Date().getTime();
                LabCronJob cronJob = new LabCronJob("lab_operation", "0", time, 1);
                labCronJobService.save(cronJob);
            }
        }





    }

}